#!/bin/bash

if [ -z "$1" ]; then
    echo "Usage:"
    echo "  issue.sh add <issueNO>    - Add issue demo to build"
    echo "  issue.sh clear            - Clear all issue configurations"
    echo "  issue.sh new <typeName-issueNO-desc>    - Create new issue demo"
    echo "  issue.sh buildall         - Build demos hap with all issues"
    echo "  issue.sh build <issueNO>  - Build demos hap with specified issues"
    exit 1
fi

if [ "$1" == "add" ]; then
    if [ -z "$2" ]; then
        echo "Error: issueNO is required"
        echo "Example: issue.sh add 20250001"
        exit 1
    fi
    echo "Adding issue demo $2..."
    hvigorw issueAdd -p issueNO=$2
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    exit 0
fi

if [ "$1" == "clear" ]; then
    echo "Clearing issue configurations..."
    hvigorw issueClear
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    exit 0
fi

if [ "$1" == "new" ]; then
    if [ -z "$2" ]; then
        echo "Error: demoname is required"
        echo "Example: issue.sh new ArkUI-20250001-test"
        exit 1
    fi
    echo "New issue demo $2..."
    hvigorw generateModule -p demoname=$2
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    exit 0
fi

if [ "$1" == "buildall" ]; then
    echo "Execute clear issue demos...."
    hvigorw issueClear
    echo "Execute add all issue demos...."
    hvigorw issueAddAll
    echo "Execute install hars...."
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    echo "Execute build hap...."
    hvigorw --mode module -p module=default@default -p product=default assembleHap
    exit 0
fi

if [ "$1" == "build" ]; then
    if [ -z "$2" ]; then
        echo "Error: issueNO is required"
        echo "Example: issue.sh build 20250001"
        exit 1
    fi
    echo "Execute clear issue demos...."
    hvigorw issueClear
    echo "Execute add issue demo $2..."
    hvigorw issueAdd -p issueNO=$2
    echo "Execute install hars...."
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    echo "Execute build hap...."
    hvigorw --mode module -p module=default@default -p product=default assembleHap
    exit 0
fi

echo "Error: Unknown command '$1'"
echo "Valid commands are: add, clear, new, buildall, build"
exit 1